package top.jybill.service.impl;

import top.jybill.dao.IAccountDao;
import top.jybill.domain.Account;
import top.jybill.service.IAccountService;
import top.jybill.utils.ThreadManager;

import java.util.List;

public class AccountServiceImpl implements IAccountService {
  private IAccountDao accountDao;
  private ThreadManager threadManager;

  public void setThreadManager(ThreadManager threadManager) {
    this.threadManager = threadManager;
  }

  public void setAccountDao(IAccountDao accountDao) {
    this.accountDao = accountDao;
  }

  public List<Account> findAll() {
    return accountDao.findAll();
  }

  public Account findAccountById(Integer id) {
    return accountDao.findAccountById(id);
  }

  public void updateAccount(Account account) {
    accountDao.updateAccount(account);
  }

  public void insertAccount(Account account) {
    accountDao.insertAccount(account);
  }

  public void delAccount(Integer id) {
    accountDao.delAccount(id);
  }

  public void transfer(String accountName, String targetName, Float money) {
      // 获取发钱账户
      Account account = accountDao.findAccountByName(accountName);
      // 获取收钱账户
      Account tartgetAccount = accountDao.findAccountByName(targetName);
      // 发钱账户、收钱账户扣款/收钱操作
      account.setMoney(account.getMoney() - money);
      tartgetAccount.setMoney(tartgetAccount.getMoney() + money);
      // 保存账户
      accountDao.updateAccount(account);
//      int e = 1 / 0;
      accountDao.updateAccount(tartgetAccount);
  }
}
